package cz.kfad.ofrss.backend;


import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import cz.kfad.ofrss.R;
import cz.kfad.ofrss.db.DatabaseHelper;
import cz.kfad.ofrss.model.Category;
import cz.kfad.ofrss.model.Channel;
import cz.kfad.ofrss.model.RssFeed;
import cz.kfad.ofrss.model.RssFeedShort;
import cz.kfad.ofrss.rss.CategoriesScanner;
import cz.kfad.ofrss.rss.RssFetcher;
import cz.kfad.ofrss.rss.RssFetcherImpl;
import cz.kfad.ofrss.widget.Widget;

import java.util.List;

public class BackendImpl implements IBackend {


    private final DatabaseHelper databaseHelper;
    private final Context initContext;
    private RssFetcher rssFetcher;

    public BackendImpl(final Context initContext) {
        databaseHelper = new DatabaseHelper(initContext);
        rssFetcher = new RssFetcherImpl(initContext);
        this.initContext = initContext;
    }


    @Override
    public List<Category> findAllCategories() {
        return databaseHelper.findAllCategories();
    }

    @Override
    public List<Channel> findAllChannels() {
        return databaseHelper.findAllChannels();
    }

    @Override
    public List<RssFeedShort> findFeedsForChannel(int channelId) {
        return databaseHelper.findFeedByChannel(channelId);
    }

    @Override
    public List<RssFeedShort> findFeedsForCategory(int categoryId) {
        return databaseHelper.findFeedByCategory(categoryId);
    }

    @Override
    public RssFeed findFeed(int feedId) {
        return databaseHelper.findFeed(feedId);
    }

    @Override
    public void setFeedIsRead(int feedId) {
        databaseHelper.saveRssFeedIsRead(feedId);
        reloadWidgets();
    }

    @Override
    public Channel findChannelById(int channelId) {
        return databaseHelper.findChannelById(channelId);
    }

    @Override
    public Category findCategoryById(int categoryId) {
        return databaseHelper.findCategoryById(categoryId);
    }

    @Override
    public void refreshFeeds() {
        rssFetcher.fetchAllChannels();
        reloadWidgets();
    }

    @Override
    public void deleteFeed(int itemId) {
        databaseHelper.deleteRssFeedById(itemId);
        reloadWidgets();
    }

    @Override
    public void deleteChannel(int channelId) {
        databaseHelper.deleteChannelById(channelId);
        reloadWidgets();
    }

    @Override
    public void deleteCategory(int categoryId) {
        databaseHelper.deleteCategoryById(categoryId);
        reloadWidgets();
    }

    @Override
    public void saveCategory(Category category) {
        databaseHelper.saveCategory(category);
        final CategoriesScanner categoriesScanner = new CategoriesScanner(initContext);
        final List<RssFeed> feed = categoriesScanner.findFeed(category);
        for (final RssFeed rssFeed : feed) {
            databaseHelper.saveCategoryFeed(rssFeed.getId(), category.getId(), !rssFeed.isRead());
        }
    }

    @Override
    public void saveChannel(Channel channel) {
        databaseHelper.saveChannel(channel);
    }

    @Override
    public boolean isUrlLive(String url) {
        return rssFetcher.testUrl(url);
    }

    @Override
    public List<RssFeed> findFeeds() {
        return databaseHelper.findAllFeeds();
    }

    @Override
    public void editChannel(Channel channel) {
        databaseHelper.editChannel(channel);
    }

    @Override
    public void editCategory(Category category) {
        databaseHelper.editCategory(category);
        final CategoriesScanner categoriesScanner = new CategoriesScanner(initContext);
        final List<RssFeed> feed = categoriesScanner.findFeed(category);
        for (final RssFeed rssFeed : feed) {
            databaseHelper.saveCategoryFeed(rssFeed.getId(), category.getId(), !rssFeed.isRead());
        }
    }

    @Override
    public List<RssFeedShort> findAllFeeds(String orderBy) {
        return databaseHelper.findAllFeeds(orderBy);
    }

    public void reloadWidgets() {
        Intent intent = new Intent(initContext, Widget.class);
        intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
        // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
        // since it seems the onUpdate() is only fired on that:
        int[] ids = {R.xml.widget_info};
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
        initContext.sendBroadcast(intent);
    }

    public void fetchChannel(Channel channel) {
        rssFetcher.fetchChannel(channel);
    }

    @Override
    public int getCountUnreadFeeds() {
        return databaseHelper.calculateCountUnreadFeeds();
    }
}
